<?php
  include("include/conn.php");
  
  if(isset($_REQUEST['kaarigarId']))
    $totalId = $_REQUEST['kaarigarId'];
  else
	  $totalId = $_REQUEST['total_ids'];
	  
	$totalId=split(",",$totalId);
	$deleteId="";
	for($i=0;$i < count($totalId);$i++) //to skip extra "0" that we have taken when passing form argument
	{

    if(isset($_REQUEST['kaarigarId']))
 		  @$checkboxvalue= $_REQUEST['kaarigarId'][$i];
    else
		  @$checkboxvalue=$_REQUEST['C1_'.$totalId[$i]];

		if(isset($checkboxvalue))
		{
			$deleteId.=",".$totalId[$i];
		}
	}
	if($deleteId!="")
	{
		$deleteId=substr($deleteId,1);

    $selectKaarigar = "SELECT kaarigardetail.kaarigarDate, kaarigardetail.particulars, kaarigardetail.crAmount, kaarigardetail.drAmount, kaarigar.kaarigarName
                         FROM kaarigardetail
                         JOIN kaarigar ON kaarigar.kaarigarId = kaarigardetail.kaarigarId
                        WHERE kaarigardetail.inRojmel = 1
                          AND kaarigar.kaarigarId IN (".$deleteId.")";
    $selectKaarigarRes = mysql_query($selectKaarigar);
    while($row = mysql_fetch_assoc($selectKaarigarRes))
    {
    	$crDr = "";
    	$amount = 0;
    	if($row['crAmount'] > 0)
    	{
    	  $amount = $row['crAmount'];
    	  $crDr   = "Cr";
    	}
    	else if($row['drAmount'] > 0)
    	{
    	  $amount = $row['drAmount'];
    	  $crDr   = "Dr";
    	}
    	  
    	$insertRojmel = "INSERT INTO rojmel (rojmelDate, particulars, amount, CrDr)
    	                 VALUES ('".$row['kaarigarDate']."','".$row['particulars']."(".$row['kaarigarName'].")',".$amount.",'".$crDr."')";
      $insertRojmelRes = mysql_query($insertRojmel);
    }
    
	  $query1 = "DELETE FROM kaarigar 
	              WHERE kaarigarId IN (".$deleteId.")";
	  $result1 = mysql_query($query1);
	  
	  $query2 = "DELETE FROM kaarigardetail
	              WHERE kaarigarId IN (".$deleteId.")";
	  $result2 = mysql_query($query2);
	  
	}
	
	mysql_close($con) or die("error closing connection");
  header("Location:kaarigarList.php");
	exit();
 ?>